package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public BusinessDataVO getBusinessData() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        Integer newUsers = reportMapper.dayNewUserCount(map);
        newUsers = newUsers == null ? 0 : newUsers;
        Double turnover = reportMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        Integer validOrderCount = reportMapper.dayValidCount(map);
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;
        Integer orderCount = reportMapper.dayCount( map);
        orderCount = orderCount == null ? 0 : orderCount;
        double orderCompletionRate = 0.0;
        if (orderCount != 0){
             orderCompletionRate = validOrderCount.doubleValue() / orderCount;
        }
        Integer totalUserCount = reportMapper.dayTotalUserCount(map);
        double unitPrice = 0.0;
        if(totalUserCount != 0){
             unitPrice = turnover / totalUserCount;
        }
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = reportMapper.dayCount(map);
        map.remove("status", Orders.PENDING_PAYMENT);
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = reportMapper.dayCount(map);
        map.remove("status", Orders.COMPLETED);
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = reportMapper.dayCount(map);
        map.remove("status", Orders.CANCELLED);
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = reportMapper.dayCount(map);
        map.remove("status", Orders.CONFIRMED);
        Integer allOrders = reportMapper.dayCount(map);
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeal() {
        Integer sold = setmealMapper.getnumberByStatus(1);
        sold = sold == null ? 0 : sold;
        Integer discontinued = setmealMapper.getnumberByStatus(0);
        discontinued = discontinued == null ? 0 : discontinued;
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Integer  sold = dishMapper.getnumberByStatus(1);
        sold = sold == null ? 0 : sold;
        Integer discontinued = dishMapper.getnumberByStatus(0);
        discontinued = discontinued == null ? 0 : discontinued;
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
